//
//  HYBaseWebViewController.m
//  Booking
//
//  Created by Duntech on 2018/5/8.
//  Copyright © 2018年 Duntech. All rights reserved.
//

#import "HYBaseWebViewController.h"
#import "HYBookingRoomFilterView.h"

@interface HYBaseWebViewController () <WKUIDelegate,WKNavigationDelegate>

@property (nonatomic,strong) HYBookingRoomFilterView *filterView;
/** webView */
@property (nonatomic,strong)  WKWebView *webView;

@end

@implementation HYBaseWebViewController

- (instancetype)initWithUrl:(NSString *)url{
    
    if (self = [super init]) {
        
        self.url = url;
    }
    return self;
}

- (void)dealloc{
    
    if (_webView) {
        
        [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
    }
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self.view addSubview:self.webView];
    [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)backBtnAction{
    
    if ([self.webView canGoBack]) {
        
        [self.webView goBack];
    }
    else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - 监听加载进度
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    
    if (object == self.webView &&  [keyPath isEqualToString:@"estimatedProgress"]) {
        
        CGFloat newProgress = [[change objectForKey:NSKeyValueChangeNewKey] doubleValue];
        
    }
}

#pragma mark - webViewDelegate
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    
    [self.webView evaluateJavaScript:@"document.title" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
        self.title = result;
    }];
    
}

- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error{
    
    NSLog(@"%@",error);
}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    
    NSURL *URL = navigationAction.request.URL;
    NSString *scheme = [URL scheme];
    if ([scheme isEqualToString:@"tel"]) {
        NSString *resourceSpecifier = [URL resourceSpecifier];
        NSString *callPhone = [NSString stringWithFormat:@"telprompt://%@", resourceSpecifier];
        /// 防止iOS 10及其之后，拨打电话系统弹出框延迟出现
        dispatch_async(dispatch_get_main_queue(), ^{
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]];
        });
    }
    decisionHandler(WKNavigationActionPolicyAllow);
}

#pragma mark - lazyload
- (WKWebView *)webView{
    
    if (!_webView) {
        
        _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - KNAV_HEIGHT)];
        _webView.backgroundColor = [UIColor whiteColor];
        _webView.UIDelegate = self;
        _webView.navigationDelegate = self;
        [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
        
        //        //创建WKWebView的配置对象
        //        WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
        //        //设置configuration对象的preferences属性的信息
        //        WKPreferences *preference = [[WKPreferences alloc] init];
        //        configuration.preferences = preference;
        //        //是否允许与JS交互，默认YES
        //        preference.javaScriptEnabled = YES;
        //        //通过JS与WebView内容交互
        //        configuration.userContentController = [[WKUserContentController alloc] init];
        //        [configuration.userContentController addScriptMessageHandler:self name:@"callback"];
    }
    return _webView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
